package com.example.springcloudexamineserver;

import com.example.springcloudexamineserver.config.FileListen;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.HiddenFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.concurrent.TimeUnit;

@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class SpringCloudExamineServerApplication {

    public static void main(String[] args) throws Exception {

        SpringApplication.run(SpringCloudExamineServerApplication.class, args);

        //监控目录
        String rootDir = "D:\\upload";
        //间隔5秒
        long interval = TimeUnit.SECONDS.toMillis(1);
        IOFileFilter directories = FileFilterUtils.and(
                FileFilterUtils.directoryFileFilter(),
                HiddenFileFilter.VISIBLE
        );
        IOFileFilter files = FileFilterUtils.and(
                FileFilterUtils.fileFileFilter(),
                FileFilterUtils.suffixFileFilter(".onnx")
        );
        IOFileFilter filter = FileFilterUtils.or(directories,files);
        //使用过滤器
        FileAlterationObserver observe = new FileAlterationObserver(new File(rootDir), filter);
        //不使用过滤器
//        FileAlterationObserver observer = new FileAlterationObserver(new File(rootDir));
        observe.addListener(new FileListen());
        //创建文件变化监听器
        FileAlterationMonitor monitor = new FileAlterationMonitor(interval,observe);
        //开始监控
        monitor.start();
        System.out.println();
        FileAlterationListenerAdaptor listenerAdaptor = new FileListen();
    }

    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate;
    }
}
